import { defineConfig, loadEnv } from "vite";
import { wrapperEnv } from "./webPatch/vite/getEnv";
import { resolve } from "path";
import vue from "@vitejs/plugin-vue";
import { createProxy } from "./webPatch/vite/proxy";

const webResource = filePath => {
  return resolve(__dirname, `./web/${filePath}`);
};

// @see: https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const root = process.cwd();
  const env = loadEnv(mode, root);
  const viteEnv = wrapperEnv(env);

  return {
    plugins: [vue()],
    resolve: {
      alias: {
        /** 以下mapping，用于 pdf.js */
        pdfjs: resolve(__dirname, "./src"),
        "pdfjs-lib": resolve(__dirname, "./src/pdf.js"),
        "pdfjs-web": resolve(__dirname, "./web"),
        "fluent-bundle": resolve(
          __dirname,
          "./node_modules/@fluent/bundle/esm/index.js"
        ),
        "fluent-dom": resolve(
          __dirname,
          "./node_modules/@fluent/dom/esm/index.js"
        ),
        "cached-iterable": resolve(
          __dirname,
          "./node_modules/cached-iterable/src/index.mjs"
        ),
        "display-fetch_stream": resolve(
          __dirname,
          "./src/display/fetch_stream.js"
        ),
        "display-network": resolve(__dirname, "./src/display/network.js"),
        "display-node_stream": resolve(__dirname, "./src/display/stubs.js"),
        "display-node_utils": resolve(__dirname, "./src/display/stubs.js"),
        "web-alt_text_manager": webResource("./alt_text_manager.js"),
        "web-annotation_editor_params": webResource(
          "./annotation_editor_params.js"
        ),
        "web-download_manager": webResource("./download_manager.js"),
        "web-external_services": webResource("./genericcom.js"),
        "web-null_l10n": webResource("./genericl10n.js"),
        "web-pdf_attachment_viewer": webResource("./pdf_attachment_viewer.js"),
        "web-pdf_cursor_tools": webResource("./pdf_cursor_tools.js"),
        "web-pdf_document_properties": webResource(
          "./pdf_document_properties.js"
        ),
        "web-pdf_find_bar": webResource("./pdf_find_bar.js"),
        "web-pdf_layer_viewer": webResource("./pdf_layer_viewer.js"),
        "web-pdf_outline_viewer": webResource("./pdf_outline_viewer.js"),
        "web-pdf_presentation_mode": webResource("./pdf_presentation_mode.js"),
        "web-pdf_sidebar": webResource("./pdf_sidebar.js"),
        "web-pdf_thumbnail_viewer": webResource("./pdf_thumbnail_viewer.js"),
        "web-preferences": webResource("./genericcom.js"),
        "web-print_service": webResource("./pdf_print_service.js"),
        "web-secondary_toolbar": webResource("./secondary_toolbar.js"),
        "web-toolbar": webResource("./toolbar.js"),

        /** 以下mapping，用于vue */
        "@": resolve(__dirname, "./webPatch"),
      },
    },
    server: {
      host: "0.0.0.0",
      port: viteEnv.VITE_PORT,
      cors: true,
      // 代理配置使用 .env.development
      proxy: createProxy(viteEnv.VITE_PROXY),
    },
  };
});
